<?php

/**
 * Base project form.
 * 
 * @package    test_cart
 * @subpackage form
 * @author     Your name here 
 * @version    SVN: $Id: BaseForm.class.php 20147 2009-07-13 11:46:57Z FabianLange $
 */
class BaseForm extends sfFormSymfony
{
    // override constructor to set default message
    public function __construct($object = null, $options = array(), $CSRFSecret = null)
    {
        sfValidatorBase::setDefaultMessage('required', 'Please insert required data');
        sfValidatorBase::setDefaultMessage('max_length', 'Filed data is too long (%max_length% characters max)');
        sfValidatorBase::setDefaultMessage('min_length', 'Filed data is too short (%min_length% characters min)');
        parent::__construct($object, $options, $CSRFSecret);
    }

    public function addFormatter($formatterName)
    {
        $customDecorator = new sfWidgetFormSchemaCustomFormatter($this->getWidgetSchema());
        $this->widgetSchema->addFormFormatter($formatterName, $customDecorator);
        $this->widgetSchema->setFormFormatterName($formatterName);
        $this->addRequiredSpan();
    }

    public function addRequiredSpan(array $ignoreArray = array())
    {
        $Validatorfileds = $this->getValidatorSchema()->getFields();
        $requiredArray   = array();
        foreach ($Validatorfileds as $name => $validator)
        {
            if ($validator->getOption("required") && array_search($name, $ignoreArray) === false)
            {
                $requiredArray[] = $name;
            }
        }
        $this->getWidgetSchema()->getFormFormatter()->setRequiredFields($requiredArray);
    }

}
